package com.zt.rongda.salary.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zt.rongda.salary.model.SalaryPayRollDetail;
import com.zt.rongda.salary.service.SalaryPayRollDetailService;
import com.zt.servlet.DeptPermValidate;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/salaryPayRollDetail")
public class SalaryPayRollDetailController {

    @Autowired
    private SalaryPayRollDetailService salaryPayRollDetailService;

    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(salaryPayRollDetailService.getTable(reqBean));
    }
    @RequestMapping(value = "/getEmpQuotaDetail")
    @ResponseBody
    public JSONObject getEmpQuotaDetail(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
    	String empId = reqBean.getInfo("empId");
    	String quotaType ="";
    	int payRollYear = reqBean.getInfoInt("payRollYear");
    	int payRollMonth =  reqBean.getInfoInt("payRollMonth");
    	return Response.response(salaryPayRollDetailService.getEmpQuotaDetail(empId,quotaType,payRollYear,payRollMonth));
    }
    
    @RequestMapping(value = "/analyzeEmpSalary")
    @ResponseBody
    public JSONObject analyzeEmpSalary(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
    	String empId = reqBean.getInfo("empId");
    	String payRollYear = reqBean.getInfo("payRollYear");
    	return Response.response(salaryPayRollDetailService.analyzeEmpSalary(empId,payRollYear));
    }
    
    @RequestMapping(value = "/getEmpByEmpName")
    @ResponseBody
    public JSONObject getEmpByEmpName(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
    	String empName = reqBean.getInfo("empName");
    	return Response.response(salaryPayRollDetailService.getEmpByEmpName(empName));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    @DeptPermValidate(id = "paySlipId", deptId = "deptId")
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        salaryPayRollDetailService.add(getSalaryPayRollDetail(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    @DeptPermValidate(id = "paySlipId", deptId = "deptId")
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        salaryPayRollDetailService.update(getSalaryPayRollDetail(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    @DeptPermValidate(id = "paySlipId", deptId = "deptId")
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        salaryPayRollDetailService.delete(reqBean.getInfo("paySlipId"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private SalaryPayRollDetail getSalaryPayRollDetail(ReqBean reqBean) {
        SalaryPayRollDetail salaryPayRollDetail = new SalaryPayRollDetail();
        salaryPayRollDetail.setPaySlipId(reqBean.getInfo("paySlipId"));
        salaryPayRollDetail.setPayrollId(reqBean.getInfo("payrollId"));
        salaryPayRollDetail.setEmpId(reqBean.getInfo("empId"));
        salaryPayRollDetail.setEmpName(reqBean.getInfo("empName"));
        salaryPayRollDetail.setDeptId(reqBean.getInfo("deptId"));
        salaryPayRollDetail.setDeptName(reqBean.getInfo("deptName"));
        salaryPayRollDetail.setBaseSalary(reqBean.getInfoDouble("baseSalary"));
        salaryPayRollDetail.setWorkingAgeSubsidy(reqBean.getInfoDouble("workingAgeSubsidy"));
        salaryPayRollDetail.setPositionSalary(reqBean.getInfoDouble("positionSalary"));
        salaryPayRollDetail.setDeduction(reqBean.getInfoDouble("deduction"));
        salaryPayRollDetail.setAttendance(reqBean.getInfoDouble("attendance"));
        salaryPayRollDetail.setAttendanceDeductions(reqBean.getInfoDouble("attendanceDeductions"));
        salaryPayRollDetail.setPayableSalary(reqBean.getInfoDouble("payableSalary"));
        salaryPayRollDetail.setSocialSecurity(reqBean.getInfoDouble("socialSecurity"));
        salaryPayRollDetail.setTax(reqBean.getInfoDouble("tax"));
        salaryPayRollDetail.setPaidSalary(reqBean.getInfoDouble("paidSalary"));
        salaryPayRollDetail.setQuotaJson(reqBean.getInfo("quotaJson"));
        salaryPayRollDetail.setStatus(reqBean.getInfo("status"));
        salaryPayRollDetail.setOperator(reqBean.getUserId());
        salaryPayRollDetail.setRemark(reqBean.getInfo("remark"));
        return salaryPayRollDetail;
    }

}